package com.itheima.mall.service.smsService;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.sms.SmsHomeBrand;
import com.itheima.mall.commons.PageResult;
import com.itheima.mall.mapper.smsMapper.SmsHomeBrandMapper;
import com.itheima.mall.service.smsService.SmsHomeBrandServiceApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandServiceApiImpl implements SmsHomeBrandServiceApi {

    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;
    /**
     * 添加首页推荐品牌
     *homeBrandList
     */
    @Override
    public void create(SmsHomeBrand smsHomeBrand) {
        smsHomeBrandMapper.insert(smsHomeBrand);
    }

    /**
     * 批量删除推荐品牌
     *ids
     */
    @Override
    public void deleteBrand(List<Long> ids) {
        smsHomeBrandMapper.deleteBatchIds(ids);
    }

    /**
     * 分页查询品牌推荐
     * pageNum pageSize brandName recommendStatus
     */
    @Override
    public PageResult<SmsHomeBrand> getBrandByPage(String brandName, Integer recommendStatus, Integer pageNum, Integer pageSize) {
        IPage<SmsHomeBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> lqw =new LambdaQueryWrapper<>();
        lqw.eq(recommendStatus!=null,SmsHomeBrand::getRecommendStatus,recommendStatus);
        lqw.like(StrUtil.isNotEmpty(brandName),SmsHomeBrand::getBrandName,brandName);
        smsHomeBrandMapper.selectPage(page,lqw);
        PageResult<SmsHomeBrand> pageBean = new PageResult<>(page);
        return pageBean;

    }

    /**
     * 批量修改推荐品牌状态
     * ids recommendStatus
     */
    @Override
    public void updateBrand(List<Long> ids, Integer recommendStatus) {
        for (Long id : ids) {
            SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
            smsHomeBrand.setId(id);
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeBrandMapper.updateById(smsHomeBrand);
        }
    }

    /**
     * 修改推荐品牌排序
     *id sort
     */
    @Override
    public void updateSortBrand(Long id, Integer sort) {
        SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
        smsHomeBrand.setId(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }
}
